'use strict';
const http = require('http')
const url = require('url')
function createApplication() {
    let app = function (req, res) {
        let { pathname } = url.parse(req.url, true);
        let route = app.routes[pathname];
        let r = { "req": req, "res": res, "path": pathname, "app": app, method: req.method.toLocaleUpperCase() };
        try {
            return route ? route.apply(r) : app.error404(r);
        }
        catch (err) {
            app.error(Object.assign(r, { err: err.toString(), errCode: 500 }));
        }
    };
    app.routes = {};
    app.error = (r) => {
        console.error({ "path": r.path, err: r.err, errCode: r.errCode });
        r.res.writeHead(r.errCode, { 'Content-Type': 'text/html' });
        r.res.write(r.errCode.toString());
        r.res.end();
    };
    app.error404 = (r) => app.error(Object.assign(r, { err: "path not find", errCode: 404 }));
    app.listen = function () {
        let server = http.createServer(app);
        server.listen(...arguments);
        return server;
    };
    return app;
}
module.exports = createApplication;